﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Interactivity;
using Microsoft.Phone.Shell;
using nRoute.SiteMaps;
using System.Windows.Data;
using System.ComponentModel;
using Microsoft.Phone.Controls;
using System.Collections.Specialized;
using System.Windows.Controls;

namespace nRoute.Behaviors
{
    public class BridgeApplicationBarBehavior
        : Behavior<FrameworkElement>
    {
        private NodesCollection _primaryNodes;
        private NodesCollection _secondaryNodes;
        
        public static readonly DependencyProperty ApplicationBarProperty = DependencyProperty.Register("ApplicationBar", 
            typeof(IApplicationBar), typeof(BridgeApplicationBarBehavior), new PropertyMetadata(null, OnApplicationBarChanged));
        
#region Properties

        [Category("Common Properties")]
        public IApplicationBar ApplicationBar 
        {
            get { return (IApplicationBar)GetValue(ApplicationBarProperty); }
            set { SetValue(ApplicationBarProperty, value); }
        }

#endregion

#region Overridable

        protected override void OnAttached()
        {
            base.OnAttached();

            var _phonePage = this.AssociatedObject as PhoneApplicationPage;
            if (_phonePage != null)
            {
                if (this.ApplicationBar != null && _phonePage.ApplicationBar == null)
                {
                    _phonePage.ApplicationBar = this.ApplicationBar;
                }
                else if (this.ApplicationBar == null && _phonePage.ApplicationBar != null)
                {
                    this.ApplicationBar = _phonePage.ApplicationBar;
                }
                else if (this.ApplicationBar == null && _phonePage.ApplicationBar == null)
                {
                    _phonePage.ApplicationBar = this.ApplicationBar = new ApplicationBar();
                }
            }

            _primaryNodes = SiteMaps.SiteMaps.GetPrimary(this.AssociatedObject);
            SetupApplicationBarButtons(this.ApplicationBar, _primaryNodes);

            _secondaryNodes = SiteMaps.SiteMaps.GetSecondary(this.AssociatedObject);
            SetupApplicationBarMenuItems(this.ApplicationBar, _secondaryNodes);
        }

#endregion

#region Handlers

        private static void OnApplicationBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var _behavior = (BridgeApplicationBarBehavior)d;
            _behavior.SetupApplicationBarButtons((IApplicationBar)e.NewValue, _behavior._primaryNodes);
            _behavior.SetupApplicationBarMenuItems((IApplicationBar)e.NewValue, _behavior._secondaryNodes);
        }

        private void  PrimaryNodes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
 	        
        }

        private void  SecondaryNodes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {

        }

#endregion

#region Helpers

        private void SetupApplicationBarButtons(IApplicationBar applicationBar, NodesCollection nodes)
        {
            if (applicationBar == null || nodes == null || this.AssociatedObject == null) return;
            nodes.CollectionChanged += PrimaryNodes_CollectionChanged;

            foreach (var _node in nodes)
            {
                var _buttonNode = _node;    
                var _button = new ApplicationBarIconButton();
                _button.Text = _buttonNode.Title;
                _button.IconUri = new Uri(_buttonNode.IconPath, UriKind.Relative);
                _button.IsEnabled = _buttonNode.IsEnabled;
                _button.Click += (s, e) => _buttonNode.Execute();

                applicationBar.Buttons.Add(_button);
            }
        }

        private void SetupApplicationBarMenuItems(IApplicationBar applicationBar, NodesCollection nodes)
        {
            if (applicationBar == null || nodes == null || this.AssociatedObject == null) return;
            nodes.CollectionChanged += SecondaryNodes_CollectionChanged;
        
            foreach (var _node in nodes)
            {
                var _menuNode = _node;
                var _menuItem = new ApplicationBarMenuItem();
                _menuItem.Text = _menuNode.Title;
                _menuItem.IsEnabled = _menuNode.IsEnabled;
                _menuItem.Click += (s, e) => _menuNode.Execute();

                applicationBar.MenuItems.Add(_menuItem);
            }
        }

        //public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
        //{
        //    // props I need to handle
        //    // title
        //    // icon path
        //    // is enabled
        //    // is listed
        //    // and any item removed or added

        //    Binding b = new Binding(propertyName) { Source = element };
        //    var prop = System.Windows.DependencyProperty.RegisterAttached(
        //        "ListenAttached" + propertyName,
        //        typeof(object),
        //        typeof(UserControl),
        //        new PropertyMetadata(callback));

        //    element.SetBinding(prop,  b);
        //    //element.SetBinding(prop, DependencyProperty.UnsetValue);
        //}

#endregion

    }
}